<%@page import="edu.ncsu.csc.itrust.beans.LabProcedureBean"%>
<%@page import="edu.ncsu.csc.itrust.exception.DBException"%>
<%@page import="edu.ncsu.csc.itrust.dao.mysql.LabProcedureDAO"%>
<%@page import="java.util.List"%>
<%@page import="edu.ncsu.csc.itrust.action.ViewLabTechProceduresAction"%>
<%@page import="edu.ncsu.csc.itrust.validate.LabProcedureValidator"%>
<%@page import="edu.ncsu.csc.itrust.exception.FormValidationException"%>

<%@include file="/global.jsp"%>

<%
pageTitle = "iTrust - Report Results";
%>

<%@include file="/header.jsp"%>

<%
String headerMessage = "";
LabProcedureBean lbean = null;
long requestID = 0L;
String lpid = request.getParameter("ID");

if (lpid != null && !lpid.equals("")) {
	try {		
		requestID = Long.parseLong(lpid);
		lbean = prodDAO.getLabProcedureDAO().getLabProcedure(requestID);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

LabProcedureDAO lpDAO = prodDAO.getLabProcedureDAO();
boolean formIsFilled = request.getParameter("formIsFilled") != null
	&& request.getParameter("formIsFilled").equals("true");

if (formIsFilled) {
	try{
		LabProcedureBean b = prodDAO.getLabProcedureDAO().getLabProcedure(requestID);
		ViewLabTechProceduresAction action = new ViewLabTechProceduresAction(prodDAO, loggedInMID.longValue());
		b.setResults(request.getParameter("Results"));
		LabProcedureValidator valid = new LabProcedureValidator(); 
		valid.validate(b);
		action.reportResult(b.getProcedureID(), request.getParameter("Results"));
		//Results cannot be logged because they can be up to 500 in length, means a DBException.
		loggingAction.logEvent(TransactionType.LAB_RESULT_RECORD, loggedInMID.longValue(), b.getPid(), "ID:"+b.getProcedureID());
	 %><span>Information Updated Successfully</span>
<%
	}catch(FormValidationException e){
			e.printHTML(pageContext.getOut());
	}
}

%>
<br />
<br />
<table align="center">
	<tr>
		<td><%= StringEscapeUtils.escapeHtml("" + (headerMessage )) %></td>
	</tr>
</table>
<table class="fTable">
	<tr>
		<th colspan="9">Lab Procedures</th>
	</tr>
	<tr>
		<th>Lab Code</th>
		<th>Priority</th>
		<th>Rights</th>
		<th>Status</th>
		<th>Commentary</th>
		<th>Results</th>
		<th>OfficeVisitID</th>
		<th>Assigned Lab Tech</th>
		<th>Updated Date</th>

	</tr>
	<%LabProcedureBean bean = prodDAO.getLabProcedureDAO().getLabProcedure(requestID);%>
	<% String ltname = "Unassigned"; %>
	<% if(bean.getLtID() != 0){ %>
	<% ltname = prodDAO.getPersonnelDAO().getName(bean.getLtID()); %>
	<% } %>
	<tr>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getLoinc())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getPriority())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getRights())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getStatus())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getCommentary())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getResults())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getOvID())) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (ltname)) %></td>
		<td align=center><%= StringEscapeUtils.escapeHtml("" + (bean.getTimestamp())) %></td>
	</tr>
</table>
<br />
<br />
<form
	action="reportResults.jsp?ID=<%= StringEscapeUtils.escapeHtml("" + (lpid)) %>"
	&message="Updated Laboratory Procedure" method="post"><input
	type="hidden" name="formIsFilled" value="true">
<table>
	<tr>
		<td>Results:</td>
		<td><textarea name="Results"></textarea>
	</tr>
	<tr>
		<td colspan=2 align=center><input type="submit"
			style="font-size: 16pt; font-weight: bold;" value="Update"></td>
	</tr>
</table>
</form>

<%@include file="/footer.jsp"%>